package com.sdh.onlinemarket.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdh.onlinemarket.common.annotation.AdminToken;
import com.sdh.onlinemarket.common.annotation.UserLoginToken;
import com.sdh.onlinemarket.common.dto.ShopCardResultDTO;
import com.sdh.onlinemarket.common.lang.Result;
import com.sdh.onlinemarket.entity.Item;
import com.sdh.onlinemarket.entity.Shopcard;
import com.sdh.onlinemarket.service.ItemService;
import com.sdh.onlinemarket.service.ShopcardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 施德昊
 * @since 2021-11-21
 */
@Api(tags = "购物车相关模块")
@RestController
@RequestMapping("/shopcard")
@Slf4j
public class ShopcardController {

    @Autowired
    ShopcardService shopcardService;

    @Autowired
    ItemService itemService;

    @UserLoginToken
    @ApiOperation(value = "购物车添加新商品")
    @PostMapping("/addgood")
    @ResponseBody
    public Result addGood(@RequestBody Shopcard shopcard){
        if(shopcard.getItemid()==null||shopcard.getUserid()==null||shopcard.getInum()==null){
            return Result.fail("param error");
        }
        Shopcard shopcardtemp = shopcardService.getOne(new QueryWrapper<Shopcard>().eq("itemid",shopcard.getItemid()).eq("userid",shopcard.getUserid()));
        Item item = itemService.getOne(new QueryWrapper<Item>().eq("itemid",shopcard.getItemid()));

        if (item == null) {
            return Result.fail("item not found");
        }else {
            if(shopcardtemp != null){
                shopcard.setInum(shopcardtemp.getInum()+shopcard.getInum());
                if (item.getStorage() >= shopcard.getInum()){
                    shopcardService.update(shopcard,new QueryWrapper<Shopcard>().eq("itemid",shopcard.getItemid()).eq("userid",shopcard.getUserid()));
                }else {
                    log.warn("商品不足！"+shopcard.getItemid());
                    return Result.fail("item not enough");
                }
            }else {
                if (item.getStorage() >= shopcard.getInum()){
                    shopcardService.save(shopcard);
                    }else {
                    log.warn("商品不足！"+shopcard.getItemid());
                    return Result.fail("item not enough");
                }
            }

        }

        return Result.succ("add success");
    }

    @UserLoginToken
    @ApiOperation(value = "购物车删除商品")
    @PostMapping("/delgood")
    @ResponseBody
    public Result delGood(@RequestBody Shopcard shopcard){
        if(shopcard.getItemid()==null||shopcard.getUserid()==null){
            return Result.fail("param error");
        }

        boolean flag = shopcardService.remove(new QueryWrapper<Shopcard>().eq("itemid",shopcard.getItemid()));
        if(!flag){
            return Result.fail("item not exist");
        }
        return Result.succ("success");
    }

    @UserLoginToken
    @ApiOperation(value = "购物车减少商品数量")
    @PostMapping("/mingood")
    @ResponseBody
    public Result minGood(@RequestBody Shopcard shopcard){
        if(shopcard.getItemid()==null||shopcard.getUserid()==null||shopcard.getInum()==null){
            return Result.fail("param error");
        }
        Shopcard shopcardtemp = shopcardService.getOne(new QueryWrapper<Shopcard>().eq("itemid",shopcard.getItemid()));
        if(shopcardtemp == null){
            return Result.fail("item not exist");
        }
        if(shopcardtemp.getInum()-shopcard.getInum()<0){
            return Result.fail("minus error");
        }
        shopcardtemp.setInum(shopcardtemp.getInum()-shopcard.getInum());
        shopcardService.update(shopcardtemp,new QueryWrapper<Shopcard>().eq("itemid",shopcard.getItemid()));
        return Result.succ("success");
    }

    @UserLoginToken
    @ApiOperation(value = "购物车增加商品数量")
    @PostMapping("/incgood")
    @ResponseBody
    public Result incGood(@RequestBody Shopcard shopcard){
        if(shopcard.getItemid()==null||shopcard.getUserid()==null||shopcard.getInum()==null){
            return Result.fail("param error");
        }
        Shopcard shopcardtemp = shopcardService.getOne(new QueryWrapper<Shopcard>().eq("itemid",shopcard.getItemid()).eq("userid",shopcard.getUserid()));
        if(shopcardtemp == null){
            return Result.fail("item not exist");
        }
        Item itemtemp = itemService.getOne(new QueryWrapper<Item>().eq("itemid",shopcard.getItemid()));
        shopcard.setInum(shopcardtemp.getInum()+shopcard.getInum());
//        System.out.println(shopcard.getInum()+"     "+itemtemp.getStorage());
        if(shopcard.getInum() <= itemtemp.getStorage()){
            shopcardService.update(shopcard,new QueryWrapper<Shopcard>().eq("itemid",shopcard.getItemid()).eq("userid",shopcard.getUserid()));
        }else {
            log.warn("商品不足！"+shopcard.getItemid());
            return Result.fail("item not enough");
        }
        return Result.succ("success");
    }

    @UserLoginToken
    @ApiOperation(value = "修改商品数量")
    @PostMapping("/modifygood")
    @ResponseBody
    public Result ModifyGood(@RequestBody Shopcard shopcard){
        if(shopcard.getItemid()==null||shopcard.getUserid()==null||shopcard.getInum()==null){
            return Result.fail("param error");
        }
        Shopcard shopcardtemp = shopcardService.getOne(new QueryWrapper<Shopcard>().eq("itemid",shopcard.getItemid()).eq("userid",shopcard.getUserid()));
        if(shopcardtemp == null){
            return Result.fail("item not exist");
        }
        Item itemtemp = itemService.getOne(new QueryWrapper<Item>().eq("itemid",shopcard.getItemid()));
        if(shopcard.getInum() <= itemtemp.getStorage()){
            shopcardService.update(shopcard,new QueryWrapper<Shopcard>().eq("itemid",shopcard.getItemid()).eq("userid",shopcard.getUserid()));
        }else {
            log.warn("商品不足！"+shopcard.getItemid());
            return Result.fail("item not enough");
        }
        return Result.succ("success");
    }

    @UserLoginToken
    @ApiOperation(value = "获取购物车所有商品")
    @PostMapping("/getallgoods")
    @ResponseBody
    public Result getAllGoods(@RequestBody Shopcard shopcard){
        if(shopcard.getUserid()==null){
            return Result.fail("param error");
        }
        List<Shopcard> shopcardList = shopcardService.list(new QueryWrapper<Shopcard>().eq("userid",shopcard.getUserid()));
        List<ShopCardResultDTO> shopCardResultDTOList = new ArrayList<>();
        for (Shopcard shopcardt:shopcardList){
            ShopCardResultDTO shopCardResultDTOtemp = new ShopCardResultDTO();
            BeanUtils.copyProperties(shopcardt,shopCardResultDTOtemp);
            System.out.println(shopCardResultDTOtemp);
            Item item = itemService.getOne(new QueryWrapper<Item>().eq("itemid",shopcardt.getItemid()));
            shopCardResultDTOtemp.setSprice(item.getIprice());
            shopCardResultDTOtemp.setItemname(item.getItemname());
            shopCardResultDTOList.add(shopCardResultDTOtemp);
        }
        return Result.succ("success",shopCardResultDTOList);
    }

    @UserLoginToken
    @ApiOperation(value = "获取购物车总价")
    @PostMapping("/gettprice")
    @ResponseBody
    public Result getTotalPrice(@RequestBody Shopcard shopcard){
        if(shopcard.getUserid()==null){
            return Result.fail("param error");
        }
        List<Shopcard> shopcardList = shopcardService.list(new QueryWrapper<Shopcard>().eq("userid",shopcard.getUserid()));
        double totalPrice = 0;
        for (Shopcard shopcardtemp:shopcardList) {
            Item itemtemp = itemService.getOne(new QueryWrapper<Item>().eq("itemid",shopcardtemp.getItemid()));
            if (itemtemp != null){
                totalPrice += itemtemp.getIprice()*shopcardtemp.getInum();
            }
        }
        return Result.succ("success",totalPrice);

    }








}
